#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302.h"
#include "Key.h"
#include "Timer0.h"

//普中开发板
//LCD乱码将J24右两针脚短接！！！！！！！！！！！！

unsigned char Anjian=0;
unsigned char Xuanze=0;
unsigned char Wei=0;
unsigned char LiangMei=0;

void Shanshuo(void)
{
		if(LiangMei==1&&Wei==0)
	{
		LCD_ShowString(1,3,"  ");
	}
	else
	{
		LCD_ShowNum(1,3,DS1302_Time[0],2);
	}
		if(LiangMei==1&&Wei==1)
	{
		LCD_ShowString(1,6,"  ");
	}
	else
	{
		LCD_ShowNum(1,6,DS1302_Time[1],2);
	}
		if(LiangMei==1&&Wei==2)
	{
		LCD_ShowString(1,9,"  ");
	}
	else
	{
		LCD_ShowNum(1,9,DS1302_Time[2],2);
	}
		if(LiangMei==1&&Wei==3)
	{
		LCD_ShowString(2,1,"  ");
	}
	else
	{
		LCD_ShowNum(2,1,DS1302_Time[3],2);
	}
		if(LiangMei==1&&Wei==4)
	{
		LCD_ShowString(2,4,"  ");
	}
	else
	{
		LCD_ShowNum(2,4,DS1302_Time[4],2);
	}
		if(LiangMei==1&&Wei==5)
	{
		LCD_ShowString(2,7,"  ");
	}
	else
	{
		LCD_ShowNum(2,7,DS1302_Time[5],2);
	}
		if(LiangMei==1&&Wei==6)
	{
		LCD_ShowString(2,16,"  ");
	}
	else
	{
		LCD_ShowNum(2,16,DS1302_Time[6],1);
	}
}
void XIANSHI_Time(void)
{
		DS1302_Read_Time();
		LCD_ShowNum(1,3,DS1302_Time[0],2);
		LCD_ShowNum(1,6,DS1302_Time[1],2);
		LCD_ShowNum(1,9,DS1302_Time[2],2);
		LCD_ShowNum(2,1,DS1302_Time[3],2);
		LCD_ShowNum(2,4,DS1302_Time[4],2);
		LCD_ShowNum(2,7,DS1302_Time[5],2);
		LCD_ShowNum(2,16,DS1302_Time[6],1);
}
void GAIBIAN_Time(void)
{
	//选择要调的时间
	if(Anjian==3)
	{
		LCD_ShowNum(1,13,Anjian,1);
		Wei++;
		if(Wei>6)
		{
			Wei=0;
		}
	}
	if(Anjian==2)
	{
		LCD_ShowNum(1,14,Anjian,1);
		Wei--;
		if(Wei>6)
		{
			Wei=6;
		}
	}
	//间隔显示
	Shanshuo();
	//调时间
	if(Anjian==4)
	{
		LCD_ShowNum(1,15,Anjian,1);
		while(1)
		{
			Shanshuo();//间隔显示
			Anjian=Key();//检测按键
			//调整时间
			//（待写）
			if(Anjian==4)
			{
				DS1302_Write_Time();//写入DS1202
				LCD_ShowNum(1,16,Anjian,1);
				Xuanze=0;//设置显示模式
				break;
			}
			
		}
	}
}
void main()
{
	Timer0Init();
	LCD_Init();
	DS1302_Init();
	DS1302_Write_Time();
	LCD_ShowNum(1,1,20,2);
	LCD_ShowChar(1,5,'.');
	LCD_ShowChar(1,8,'.');
	LCD_ShowChar(2,3,':');
	LCD_ShowChar(2,6,':');
	LCD_ShowString(2,12,"DAY");
	LCD_ShowChar(2,15,':');
	while(1)
	{
		Anjian=Key();
		if(Anjian==1)
		{
			if(Xuanze!=0)
			{
				Xuanze=0;
			}
			else
			{
				Xuanze=1;
			}
		}
		LCD_ShowNum(1,12,Xuanze,1);
		if(Xuanze==0)
		{
			XIANSHI_Time();
		}
		else if(Xuanze==1)
		{
			GAIBIAN_Time();
		}
	}
}

void Int0_Rountine(void) interrupt 1
{
	static unsigned int a=0;
	TH0=(65535-1000)/256;
	TL0=(65535-1000)%256+1;
	
	a++;
	if(a>=500)
	{
		a=0;
		LiangMei=!LiangMei;
	}
}
